//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.StandardAddIn.Statistics
{
    internal class BetaDistribution : ContinuousDistribution
    {
        
        public BetaDistribution(double alpha, double beta)
        {
            if (alpha <= 0)
                throw new ArgumentOutOfRangeException("alpha");
            if (beta <= 0)
                throw new ArgumentOutOfRangeException("beta");
            Alpha = alpha;
            Beta = beta;
        }

        public double Alpha{
            get;
            private set;}

        public double Beta{            get;
            private set;}

        private static void checkRange(double x)
        {
            if (x < 0.0 || x > 1.0)
                throw new ArgumentOutOfRangeException("x");
        }

        public override double CumulativeProbability(double x)
        {
            checkRange(x);
            return Statistician.IncompleteBeta(x, Alpha, Beta);
        }

        protected override SolverParameters GetSolverParameters(double probability)
        {
            return new SolverParameters(0.0, 1.0, 0.5);
        }

        public override double InverseCumulativeProbability(double probability)
        {
            CheckValidProperty(probability);
            if (probability == 0.0)
                return 0.0;
            if (probability == 1.0)
                return 1.0;
            return base.InverseCumulativeProbability(probability);
        }
    }
}